﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.San_Pham
{
    public partial class TimKiemNangCao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["keyword"] != null)
                {
                    List<ElectricDevice_DTO> listresultproduct = ElectricDevice_BUS.FastSearch(Request.QueryString["keyword"].ToString());
                    dlresultproducts.DataSource = listresultproduct;
                    dlresultproducts.DataBind();
                    lbresultcount.Text = listresultproduct.Count.ToString();
                    ResultSearch.Visible = true;
                }

                ListItem item = new ListItem();
                dlCurrency.DataSource = ElectricDevice_BUS.GetCurrencyOfProduct();
                dlCurrency.DataValueField = "Currency_ID";
                dlCurrency.DataTextField = "Currency_Name";
                dlCurrency.DataBind();
                item.Text = "Tất cả";
                dlCurrency.Items.Insert(0, item);

                dlParentCatalogue.DataSource = ParentCatalogue_BUS.ListKeyCatalogue();
                dlParentCatalogue.DataValueField = "ParentCatalogue_ID";
                dlParentCatalogue.DataTextField = "ParentCatalogue_Name";
                dlParentCatalogue.DataBind();
                ListItem item1 = new ListItem();
                item1.Text = "Tất cả";
                item1.Value = "0";
                dlParentCatalogue.Items.Insert(0, item1);

                dlBrand.DataSource = Brand_BUS.ListBrand();
                dlBrand.DataTextField = "Brand_Name";
                dlBrand.DataValueField = "Brand_ID";
                ListItem item2 = new ListItem();
                item2.Text = "Tất cả";
                item2.Value = "0";
                dlBrand.DataBind();
                dlBrand.Items.Insert(0, item2);

            }
        }

        protected void imSearch_Click(object sender, ImageClickEventArgs e)
        {
            int flag = 2;

            string keyword = "";
            if(tbsearch.Text != "")
            {
                keyword = tbsearch.Text;
                 flag++;
            }

            int check = 0;
            if (cbcheck.Checked)
                check = 1;

            string kindofsearch = dlKindOfSearch.Text;

            decimal minprice = 0;
            if (tbMinPrice.Text != "")
            {
                minprice = decimal.Parse(tbMinPrice.Text);
                flag++;
            }

            decimal maxprice = 0;
            if (tbMaxPrice.Text != "")
            {
                maxprice = decimal.Parse(tbMaxPrice.Text);
                flag++;
            }

            string currency = dlCurrency.SelectedValue;
            int keycatalogueid = int.Parse(dlParentCatalogue.SelectedItem.Value.ToString());
            int brandid = int.Parse(dlBrand.SelectedItem.Value.ToString());
            List<ElectricDevice_DTO> listresultproduct = ElectricDevice_BUS.SearchElectricDevice(keyword, check, kindofsearch, minprice, maxprice, currency, keycatalogueid, brandid, flag);
            dlresultproducts.DataSource = listresultproduct;
            dlresultproducts.DataBind();
            lbresultcount.Text = listresultproduct.Count.ToString();
            ResultSearch.Visible = true;
        }
    }
}
